import { Component, OnInit } from '@angular/core';
import { Router, NavigationExtras } from '@angular/router';
import { CommonPaymentPasswordPage } from '@common-components/common-payment-password/common-payment-password.page';
import { ModalController } from '@ionic/angular';
import { AuthTokenService } from '@services/auth-token.service';
import { HttpClient } from "@angular/common/http";
import { CommonNoPaymentPwdPage } from '@common-components/common-no-payment-pwd/common-no-payment-pwd.page';

@Component({
  selector: 'app-repayment',
  templateUrl: './repayment.page.html',
  styleUrls: ['./repayment.page.scss'],
})
export class RepaymentPage implements OnInit {

  constructor(
    private router: Router,
    public modalController: ModalController,
    private authTokenService:AuthTokenService,
    private http: HttpClient
    ) { }

  ngOnInit() {
  }

  backToAccount(){
    this.router.navigate(['/tabs/myAccount']);
  }

  toSuccessfully(){
    this.router.navigate(['/tabs/myAccount/installment/successfully']);
  }

  appUserId = this.authTokenService.getAppUserId();
  toPaymentPassword(){
    // this.openPaymentPassword();
    let url = '/scm/app/checkPaymentPwdExistence.do';
 
    let requestData = {
      appUserId: this.appUserId,//this.authTokenService.getAppUserId(),
    };
    this.http.post(url, requestData).subscribe((response:any) => {
  
      let body = response.responseBody;
      if (body.firstPwd) {
          this.modalController.create({
            component: CommonNoPaymentPwdPage,
            cssClass: 'no_pwd_custom_modal',
            componentProps: {
              "url":location.pathname
            }
          }).then(resolve=>{resolve.present()});
      }else{
        this.openPaymentPassword();
      }
    });
  }
  
  currentModal:any = null;
  async openPaymentPassword(){
    const modal = await this.modalController.create({
      component: CommonPaymentPasswordPage,
      cssClass: 'custom_modal',
      componentProps: {
       "payment":true,
       "url":location.pathname
      }
    });
    await modal.present();
    this.currentModal=modal;
  }
}
